<template>
	<BaseContainer>
		<aplid-base-crud
			ref="crud"
			class="w-100"
			:paramsObj="{creditCode}"
			v-bind="baseConfig"
			:tableConfig="tableConfig()"
			:operateFormate="onOperateFormate"
			:beforeOnLoadFormate="beforeOnLoadFormate"
		>
			<template v-slot:cityArea="{row}">
				{{ row.crCityName }}{{ row.crAreaName?'/'+row.crAreaName:'' }}
			</template>
			
			<template v-slot:addDialog="{ row, show }">
				<RuleForm :subsidyType="subsidyType" v-if="show" :nowRowData="row" @refresh="handleRefresh"></RuleForm>
			</template>
			<template v-slot:menuColumn="{ row }">
				<el-button
					v-if="row.finalApprovalStatus && row.finalApprovalStatus == 2"
					v-auth="'view_workflowApprovalLogic_revocation'"
					type="primary"
					link
					@click="handleCancelApply(row.applyId, row.workflowRandom)"
				>
					撤销申请
				</el-button>
				<el-button  text type="primary" v-if="row.finalApprovalStatus && row.finalApprovalStatus == 3" @click="toOpenStopAid(row)">停发</el-button>
			</template>
		</aplid-base-crud>

		<ReportAplidDialog v-model="visible" width="75%" :rowData="rowData">
			<DetailPage v-if="visible && operateType == 'detail'" @refresh="handleRefresh" :nowRowData="rowData"></DetailPage>
		</ReportAplidDialog>
		<!-- 停发 -->
		<AplidDialog :append-to-body="true" v-model="stopAidShow" width="50%" title="停发" :model="{}">
			<StopAidForm v-if="stopAidShow" :nowRowData="copyRow" @refresh="handleRefresh"></StopAidForm>
		</AplidDialog>
	</BaseContainer>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { baseConfig, tableConfig } from './config'
import RuleForm from './RuleForm/index.vue'
import DetailPage from './detail/index.vue'

import apiMod from '@/apiMod/index'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAreaObj } from '@/utils/util'
import { getStore } from '@/utils/store'
import StopAidForm from './stopAidForm/index.vue'
import ReportAplidDialog from '@/views/instOperate/components/AplidDialog/index.vue'

const props = withDefaults(
	defineProps<{
		subsidyType?: string
	}>(),
	{
		subsidyType: '6',
	}
)
const creditCode = ref('')
creditCode.value = getStore('userInfo').organizationId|| ''
if(creditCode.value=='1'){
	// admin
	creditCode.value=undefined
}
const visible = ref(false)
const rowData:any = ref({})
const operateType = ref('detail')
const crud: any = ref(null)
//  处理刷新
const handleRefresh = (query?: any) => {
	visible.value = false
	stopAidShow.value = false
	nextTick(() => {
		crud.value.refreshChange(query)
	})
}

const onOperateFormate = (data, type) => {
	return new Promise((resolve, reject) => {
		if (type == 'detail') {
			visible.value = true
			apiMod.view_ofsSubsidyApply_view(data.applyId).then(res=>{
				rowData.value = res.data || {}
			})
			operateType.value = type
		} else {
			resolve(data)
		}
	})
}

const handleCancelApply = async (id, randomNo) => {
	ElMessageBox.confirm(`请确定是否要撤销该申请?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			const params = {
				fromId: id,
				randomNo,
			}
			const res = await apiMod.view_workflowApprovalLogic_revocation(params)
			if (res.code == 0) {
				ElMessage({
					type: 'success',
					message: res.data,
				})
				handleRefresh()
			}
		})
		.catch(() => {})
}

const beforeOnLoadFormate = (query?: any) => {
	const crAreasObj: any = getAreaObj(query.authAreas, '')
	return new Promise((resolve, reject) => {
		const userInfo = getStore('userInfo') || {}
		let tempQuery = {
			...query,
			...crAreasObj,
			authAreas: undefined,
			subsidyType: props.subsidyType,
		}
		if (tempQuery.organizationId && tempQuery.organizationId != '1') {
			tempQuery.creditCode = userInfo.organizationId
		}
		resolve(tempQuery)
	})
}
let stopAidShow = ref(false)
let copyRow = reactive({})
const toOpenStopAid = (row) =>{
	stopAidShow.value = true
	copyRow = row
}
</script>
<style scoped lang="scss">
:deep(.el-dialog) {
	--el-dialog-border-radius: 10px;
}
:deep(.el-dialog__header) {
	padding: 0;
	margin: 0;
}
</style>
